﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PACS.Data;
using System.Data;

namespace WoSoft.PACS.Data.TemplateData
{
    public class OutlineCategoryDataService : Db
    {
        /// <summary>
        /// 查询所有行记录
        /// </summary>
        /// <returns></returns>
        public DataTable GetAllData()
        {
            var dt = SqlDatabase.Sql("SELECT * FROM 报告词句分类").QuerySingle<DataTable>();
            return dt;
        }

        /// <summary>
        /// 查询所有行记录
        /// </summary>
        /// <returns></returns>
        public DataTable GetAllDataBy(string id)
        {
            var sql = "SELECT a.* FROM 报告词句分类 a join 检查项目分类 b on a.ID=b.对应词语ID or a.上级ID=b.对应词语ID where b.名称 in('" + id + "','检查备注')";
            var dt = SqlDatabase.Sql(sql).QuerySingle<DataTable>();
            return dt;
        }

        /// <summary>
        /// 插入分类
        /// </summary>
        /// <param name="cname">分类名称</param>
        /// <param name="parentID">上级ID</param>
        /// <param name="comment">注释</param>
        /// <returns></returns>
        public int Insert(string cname, string parentID, string comment)
        {
            return SqlDatabase.Insert("报告词句分类")
                .Column("ID", Guid.NewGuid().ToString())
                .Column("上级ID", parentID)
                .Column("名称", cname)
                .Column("注释", comment)
                .Execute();
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="p"></param>
        /// <param name="cname"></param>
        /// <param name="parentID"></param>
        /// <param name="comment"></param>
        /// <returns></returns>
        public int Update(string id, string cname, string parentID, string comment)
        {
            return SqlDatabase.Update("报告词句分类")
                .Column("上级ID", parentID)
                .Column("名称", cname)
                .Column("注释", comment)
                .Where("ID", id)
                .Execute();
        }

        /// <summary>
        /// 根据ID获取分类结果
        /// </summary>
        /// <param name="categoryID"></param>
        /// <returns></returns>
        public DataTable GetDataTableByID(string categoryID)
        {
            return SqlDatabase.Sql("select * from 报告词句分类 where ID = @ID")
                .Parameter("ID", categoryID)
                .QuerySingle<DataTable>();
        }

        /// <summary>
        /// 根据分类删除词句分类
        /// </summary>
        /// <param name="categoryID">分类ID</param>
        /// <returns></returns>
        public int Delete(string categoryID)
        {
            return SqlDatabase.MultiResultSql.Sql("DELETE 报告词句 where 分类ID=@分类ID")
                .Parameter("分类ID", categoryID)
                .Sql(" DELETE 报告词句分类 where ID=@ID")
                .Parameter("ID", categoryID)
                .Execute();
        }
    }
}
